<template>
    <div class="container">
        <el-dialog :title="dialog.dialogType === 'edit' ? '修改区域' : '创建区域'" :visible.sync="dialog.centerDialogVisible"
            width="30%" center :lock-scroll="true" @close="hideDialog">
            <el-form ref="formData" :rules="rules" :model="regions" label-width="100px" label-position="left">
                <el-form-item label="区域：" prop="name">
                    <el-input v-model="regions.name" placeholder="请输入区域" />
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="confirm()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
    import { Warehouseareastore, Warehouseareaupdate } from "@/api/warehouse/warehouse";
    import store from "@/store";
    export default {
        props: {
            dialog: Object,
            regions: Object,
        },
        data() {
            return {
                rules: {
                    name: [{ required: true, message: "仓库不能为空", trigger: "blur" }],
                },
            };
        },

        methods: {
            hideDialog() {
                this.dialog.centerDialogVisible = false;
                this.$refs["formData"].resetFields();
            },

            //提交
            confirm() {
                this.$refs["formData"].validate((valid) => {
                    if (valid) {
                        let data = {
                            area_name: this.regions.name,
                        };
                        if (this.dialog.dialogType == "edit") {
                            data.id = this.regions.id;
                            Warehouseareaupdate(data)
                                .then((res) => {
                                    if (res.code == 0) {
                                        this.$message({
                                            message: res.msg,
                                            type: "success",
                                        });
                                        this.hideDialog();
                                        this.$emit("upData");
                                    } else {
                                        this.$message({
                                            message: res.msg,
                                            type: "error",
                                        });
                                    }
                                })
                                .catch((err) => {
                                    console.log(err);
                                });
                        } else {
                            data.house_code = this.regions.house_code;
                            Warehouseareastore(data)
                                .then((res) => {
                                    if (res.code == 0) {
                                        this.$message({
                                            message: res.msg,
                                            type: "success",
                                        });
                                        this.hideDialog();
                                        this.$emit("upData");
                                    } else {
                                        this.$message({
                                            message: res.msg,
                                            type: "error",
                                        });
                                    }
                                })
                                .catch((err) => {
                                    console.log(err);
                                });
                        }
                    }
                });
            },
            //取消
            cancel() {
                this.hideDialog();
            },
        },
    };
</script>